package com.a214.yxz.yuejian013.Fragment;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.Toast;

import com.a214.yxz.yuejian013.Adapter.ActivityAdapter;
import com.a214.yxz.yuejian013.Adapter.ContestAdapter;
import com.a214.yxz.yuejian013.Adapter.MasterAdapter;
import com.a214.yxz.yuejian013.JavaBean.Activitys;
import com.a214.yxz.yuejian013.JavaBean.BannerView;
import com.a214.yxz.yuejian013.JavaBean.Contest;
import com.a214.yxz.yuejian013.JavaBean.master;
import com.a214.yxz.yuejian013.Listener.ActivityListener;
import com.a214.yxz.yuejian013.Listener.ContestListener;
import com.a214.yxz.yuejian013.Listener.masterListener;
import com.a214.yxz.yuejian013.Model.ActivityModel;
import com.a214.yxz.yuejian013.Model.ContestModel;
import com.a214.yxz.yuejian013.Model.MasterModel;
import com.a214.yxz.yuejian013.R;

import java.util.ArrayList;
import java.util.List;

import static com.a214.yxz.yuejian013.R.drawable.b;

public class IndexFragment extends Fragment implements View.OnClickListener,ActivityListener,masterListener,ContestListener
{
	private int[] imgs = {R.drawable.a, b,R.drawable.c,R.drawable.d};
	private List<View> viewList;
	BannerView bannerView;
	String userid;
	private RecyclerView recyclerView;
    private RadioButton index_activity;
	private RadioButton  index_master;
	private RadioButton index_match;
	List<Activitys> list;
	List<master> masterList;
	List<Contest> contestList;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState)
	{

		View view=inflater.inflate(R.layout.fragment_main, container, false);
		iniViews(view);
		viewList = new ArrayList<View>();
		for (int i = 0; i < imgs.length; i++) {
			ImageView image = new ImageView(getContext());
			image.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
			image.setScaleType(ImageView.ScaleType.CENTER_CROP);
			image.setImageResource(imgs[i]);
			viewList.add(image);
		}
		bannerView = (BannerView) view.findViewById(R.id.banner);
		bannerView.startLoop(true);
		bannerView.setViewList(viewList);

		index_activity.setOnClickListener(this);
		index_master.setOnClickListener(this);
		index_match.setOnClickListener(this);
		return view;
	}

	public void iniViews(View view){
		recyclerView= (RecyclerView) view.findViewById(R.id.id_fragment_main);
		recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
		recyclerView.setItemAnimator(new DefaultItemAnimator());
		index_activity= (RadioButton) view.findViewById(R.id.index_activity);
		index_master= (RadioButton) view.findViewById(R.id.index_master);
		index_match= (RadioButton) view.findViewById(R.id.index_match);

	}

	@Override
	public void onClick(View v) {
		SharedPreferences user=getActivity().getSharedPreferences("Userid",0);
		userid=user.getString("user_id","");
		switch (v.getId()){
			case R.id.index_activity:
				ActivityModel activityModel=new ActivityModel();
				activityModel.getActivitys(userid,1,this);
				break;
			case R.id.index_master:
				MasterModel masterModel=new MasterModel();
				masterModel.getMaster(userid,1,this);
				break;
			case R.id.index_match:
				ContestModel contestModel=new ContestModel();
				contestModel.getContest(userid,1,this);
				break;
		}
	}

	@Override
	public void onResponse(List<Activitys> bean) {
		list=bean;


		recyclerView.setAdapter(new ActivityAdapter(getActivity(),list));
	}


	@Override
	public void onFail(String msg) {
		Toast.makeText(getActivity(), ""+msg, Toast.LENGTH_SHORT).show();
	}


	@Override
	public void onResponseMaster(List<master> bean) {
		masterList=bean;
		recyclerView.setAdapter(new MasterAdapter(getActivity(),masterList));
	}

	@Override
	public void onFailMaster(String msg) {
		Toast.makeText(getActivity(), ""+msg, Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onResponseContest(List<Contest> bean) {
		contestList=bean;
		recyclerView.setAdapter(new ContestAdapter(getActivity(), contestList));

	}

	@Override
	public void onFailContest(String msg) {
		Toast.makeText(getActivity(), ""+msg, Toast.LENGTH_SHORT).show();
	}

}
